﻿using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domian.Entities.Account;
using B.S.BasicData.Domian.Entities.RBAC;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Security.Cryptography;
using System.Xml.Linq;
using System;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class UserShowHandler : IRequestHandler<UserShowCommand, ApiPaging<UserDTO>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<ShengModel> shengRepo;
        private readonly IBaseRepository<ShiModel> shiRepo;
        private readonly IBaseRepository<XianModel> xianRepo;
        private readonly IBaseRepository<UserRoleModel> userroleRepo;
        private readonly IBaseRepository<RoleModel> roleRepo;

        public UserShowHandler(IBaseRepository<AccountModel>accountRepo,IBaseRepository<ShengModel>shengRepo,IBaseRepository<ShiModel>shiRepo,IBaseRepository<XianModel>xianRepo,IBaseRepository<UserRoleModel>userroleRepo,IBaseRepository<RoleModel>roleRepo)
        {
            this.accountRepo = accountRepo;
            this.shengRepo = shengRepo;
            this.shiRepo = shiRepo;
            this.xianRepo = xianRepo;
            this.userroleRepo = userroleRepo;
            this.roleRepo = roleRepo;
        }
        public Task<ApiPaging<UserDTO>> Handle(UserShowCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiPaging<UserDTO>();
            var account = accountRepo.GetAll().Where(x => x.IsDel == false);
            var sheng = shengRepo.GetAll();
            var shi = shiRepo.GetAll();
            var xian = xianRepo.GetAll();
            var role = roleRepo.GetAll();
            var userrole = userroleRepo.GetAll();
            var linq = (from a in account
                       join b in sheng on a.Shengid equals b.Shengid
                       join c in shi on a.Shiid equals c.Shiid
                       join d in xian on a.xiangid equals d.xiangid
                       select new UserDTO
                       {
                            Id           =a.Id           ,
                            Shengid      =a.Shengid      ,
                            ShengName    =b.ShengName    ,
                            Shiid        =a.Shiid        ,
                            ShiName      =c.ShiName      ,
                            xiangid      =a.xiangid      ,
                            XianName     =d.XianName     ,
                            bid          =a.bid          ,
                            Name         =a.Name         ,
                            Sex          =a.Sex          ,
                            Addr         =a.Addr         ,
                            CreateTime   =a.CreateTime   ,
                            CreatePeople =a.CreatePeople ,
                            UpdateTime   =a.UpdateTime   ,
                            UpdatePeople =a.UpdatePeople ,
                            RoleName     =""    ,
                             
                       }).ToList();
            foreach ( var b in linq)
            {
                var ridlist=userroleRepo.GetAll().Where(x=>x.UserId==b.Id).Select(x=>x.RoleId).ToList();
                var rolelist = roleRepo.GetAll().Where(x => ridlist.Contains(x.RoleId)).Select(x => x.RoleName).ToList();
                b.RoleName=string.Join("、", rolelist);
            }
            var totalcount = linq.Count();
            var totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.PageSize);
            linq = linq.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            res.totalcount = totalcount;
            res.totalpage = totalpage;
            res.Message = "显示成功";
            res.Code = ApiEnum.Ok;
            res.Data = linq.ToList();
            return Task.FromResult(res);
        }
    }
}
